package com.ss.android.download;

import android.app.Service;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.text.TextUtils;
import android.widget.Toast;

import com.bytedance.common.utility.Logger;

public class DownloadHandlerService extends Service {
    private t a;

    public DownloadHandlerService() {

        this.a = null;
    }

    public IBinder onBind(Intent arg2) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
        if (android.support.design.a.o() != null) {
            android.support.design.a.o().f(((Context) this));
        }
    }

    public int onStartCommand(Intent arg13, int arg14, int arg15) {
        long v0_4;
        int v0_3;
        long v10 = -1;
        int v9 = 2;
        String[] v2 = null;
        super.onStartCommand(arg13, arg14, arg15);
        if (Logger.debug()) {
            Logger.d("DownloadHandlerService", "onStartCommand");
        }

        if (this.a == null) {
            this.a = p.a(((Context) this));
        }

        String v0 = arg13.getAction();
        if (!TextUtils.isEmpty(((CharSequence) v0))) {
            if (v0.equals("android.ss.intent.action.DOWNLOAD_WAKEUP")) {
                DownloadService.a(((Context) this));
            } else if (!v0.equals("android.ss.intent.action.DOWNLOAD_OPEN")
                    && !v0.equals("android.ss.intent.action.DOWNLOAD_DELETE")
                    && !v0.equals("android.ss.intent.action.DOWNLOAD_HIDE")) {
                if (v0.equals("android.ss.intent.action.DOWNLOAD_CLICK")) {
                    long v6 = ContentUris.parseId(arg13.getData());
                    String v8 = arg13.getStringExtra("extra_notification_tag");
                    Cursor v1 = k.a(this).a(ContentUris.withAppendedId(com.ss.android.download.a.a.a, v6), v2, null, v2, null);
                    try {
                        if (v1.moveToFirst()) {
                            v0_3 = g.a(v1, "status");
                            e.a(this, e.a(v0_3), v6);
                            if (com.ss.android.download.a.a.c(v0_3)) {
                                g.a(this).a(this, v6);
                                g.a(this).a(v8);
                            }
                        } else {
                            Logger.w("DownloadHandlerService", "Missing details for download " + v6);
                        }
                    } catch (Exception v0_1) {
                        v0_1.printStackTrace();
                    } finally {
                        if (v1 != null) {
                            try {
                                v1.close();
                            } catch (Exception v0_1) {

                            }
                        }
                    }
                } else if (v0.equals("android.ss.intent.action.DOWNLOAD_COMPLETE")) {
                    v0_4 = arg13.getLongExtra("extra_download_id", v10);
                    if (!g.a(this).a(v0_4)) {
                        int v3 = arg13.getIntExtra("status", -1);
                        int v4 = arg13.getIntExtra("extra_download_visibility", 1);
                        if (v0_4 <= v10) {
                        } else if (v4 != v9) {
                            android.support.design.a.a(((Context) this), v0_4, ((String) null));
                            com.ss.android.common.download.g v2_1 = android.support.design.a.v;
                            if (v2_1 == null) {
                            } else if (com.ss.android.download.a.a.a(v3)) {
                                v2_1.a(v0_4, 1);
                            }
                        }
                    }
                }
            } else {
                v0 = arg13.getAction();
                try {
                    if ("android.ss.intent.action.DOWNLOAD_DELETE".equals(v0)) {
                        Uri v0_5 = arg13.getData();
                        Intent v1_2 = new Intent(this, DownloadDeleteActivity.class);
                        v1_2.setData(v0_5);
                        v1_2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        ((Context) this).startActivity(v1_2);
                        g.a(this).a(this, ContentUris.parseId(arg13.getData()));
                    } else if ("android.ss.intent.action.DOWNLOAD_OPEN".equals(v0)) {
                        v0_4 = ContentUris.parseId(arg13.getData());
                        if (!android.support.design.a.a(((Context) this), v0_4, null)) {
                            Toast.makeText(this, "Open Fail!", Toast.LENGTH_SHORT).show();
                        }
                        g.a(this).a(this, v0_4);
                    } else if (!"android.ss.intent.action.DOWNLOAD_HIDE".equals(v0)) {
                    } else {
                        g.a(this).a(this, ContentUris.parseId(arg13.getData()));
                    }
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }

        this.stopSelf();
        return Service.START_NOT_STICKY;
    }
}

